<?php
/**
 * Created by PhpStorm.
 * User: gakuin
 * Date: 16/8/28
 * Time: 上午10:11
 */
namespace app\controllers;

use app\models\User;
use yii\web\Controller;
use app\models\ResetPwdStep1Form;
use app\models\ResetPwdStep2Form;
require_once('../utils/CCPRestSDK.php');
use app\models\ValidateCode;
use app\common\CommonHelper;
use app\models\ResetPwdStep3Form;

class ResetPwdController extends Controller{
    public $layout = 'resetlayout';
    public function actionStep1(){

        $model = new ResetPwdStep1Form();
        if($model->load(\Yii::$app->request->post())&&$model->validate()){
            $username = \Yii::$app->request->post()['ResetPwdStep1Form']['username'];
            $this->redirect('step2'."?username=$username");
        }
        return $this->render('step1',['model'=>$model]);
    }

    public function actionStep2($username){
        $model = new ResetPwdStep2Form();
        $tel = User::find()->where(['or',"username = '$username'","mobile = '$username'"])->select('mobile')->scalar();
        $mobilestr = '通过手机*******'.substr($tel,-4);
        $mobile = $tel;
        $model->mobile= $mobile;
        if($model->load(\Yii::$app->request->post())&&$model->validate()){
            $this->redirect('step3'."?username=$username");
        }
        return $this->render('step2',['model'=>$model,'mobilestr'=>$mobilestr,'mobile'=>$mobile]);
    }

    public function actionStep3($username){
        $model = new ResetPwdStep3Form();
        $data = \Yii::$app->request->post();
        if($model->load(\Yii::$app->request->post()) && $model->validate()){
            //验证成功,重新设置新密码
            $pwd = User::find()->where(['username'=>$username])->one();
            $pwd->password = \Yii::$app->getSecurity()->generatePasswordHash($data['ResetPwdStep3Form']['password']);
            $pwd->save();
            return $this->redirect(['index.php/web/home']);
        }
        return $this->render('step3',['model'=>$model]);
    }

    public function actionSms(){

        // 初始化REST SDK
//        $request = \Yii::$app->request;
//        $tel = $request->post('tel');


//主帐号
        $accountSid = '8aaf0708567749f00156789bb7f80282';

//主帐号Token
        $accountToken = '2b9a2a1a7d0a436fa6476a3a2035d812';

//应用Id
        $appId = '8aaf07085697eda30156a10a5dbb06d6';
        //$appId = '8a216da85697f5420156a1d606ed089e' 这是生产环境ps.susnion的ID

//请求地址，格式如下，不需要写https://
        $serverIP = 'app.cloopen.com';

//请求端口
        $serverPort = '8883';

//REST版本号
        $softVersion = '2013-12-26';

        $rest = new \REST($serverIP,$serverPort,$softVersion);

        $rest->setAccount($accountSid,$accountToken);
        $rest->setAppId($appId);




        //接受前端传来的手机号;
        $tel = $_POST['tel'];


        //生成验证码,然后存到验证码表中,
        $verify = (string)rand(1000,9999);


        $codeModel = new ValidateCode();

        $codeModel->create_time = CommonHelper::getCurrentTime();
        $codeModel->mobile = $tel;
        $codeModel->verify_code = $verify;
        $codeModel->save();

        $result = $rest->sendTemplateSMS($tel,[$verify,'1'],110309);//这个是 wap.hackerc.cn/web/home 的模板ID个APPID
        //$result = $rest->sendTemplateSMS($tel,[$verify,'1'],110416);//这个是生产环境的模板ID
        if ($result == NULL) {
            echo "result error!";
            return;
        }
        if ($result->statusCode != 0) {
            echo "error code :" . $result->statusCode . "<br>";
            echo "error msg :" . $result->statusMsg . "<br>";

        } else {
            echo "Sendind TemplateSMS success!<br/>";
            // 获取返回信息
            $smsmessage = $result->TemplateSMS;
            echo "dateCreated:" . $smsmessage->dateCreated . "<br/>";
            echo "smsMessageSid:" . $smsmessage->smsMessageSid . "<br/>";
        }



    }
}